package it.uniroma2.art.owlart.langsearch;

import it.uniroma2.art.owlart.exceptions.ModelAccessException;
import it.uniroma2.art.owlart.langsearch.index.config.StandardAnalysisConfiguration;
import it.uniroma2.art.owlart.langsearch.search.IndexAccessException;
import it.uniroma2.art.owlart.langsearch.search.OntIndexSearcher;
import it.uniroma2.art.owlart.protegeimpl.factory.ARTModelFactoryProtegeImpl;
import it.uniroma2.art.owlart.protegeimpl.models.OWLModelProtegeImpl;
import it.uniroma2.art.owlart.protegeimpl.start.AGROVOC_INFO;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.util.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.stanford.smi.protegex.owl.model.OWLModel;

/**
 * This class accesses MySQL-based AGROVOC ontology. It can be used to produce the CUSTOM indexes which are
 * created from instances of C_NOUN, according to the particular multilingual knowledge organization of first
 * OWL version of AGROVOC ontology<br/>
 * In addition to this, this class contains explicit Protege API code, which is used to manipulate results
 * which are returned by the OntologyIndexer in terms of OWL ART API.
 * 
 * 
 * @author Armando Stellato <stellato@info.uniroma2.it>
 * 
 */
public class AGROVOC_ADDLANGUAGE_TestOld {

	public final static String c_nounInstancesIndexCategory = "c_nounInstances";
	public final static String index_scopeNotes = "scopeNotes";
	public final static String index_ImgDescription = "imgDescription";
	public final static String index_definition = "definition";

	protected static Logger logger = LoggerFactory.getLogger(AGROVOC_ADDLANGUAGE_TestOld.class);

	protected static String genericAIMSBaseNS = "http://aims.fao.org/aos/";
	// used for all AGROVOC specific domain concepts
	protected static String agrovocBaseNS = "http://aims.fao.org/aos/agrovoc/";
	// application concepts used by CONCEPT SERVER 1.0
	protected static String commonAIMBaseNS = "http://aims.fao.org/aos/common/";

	protected static String dbusername = "root";
	protected static String dbpwd = "mysqlstarred";

	protected static OWLSearchEngine oi;
	protected static OWLModelProtegeImpl model;
	protected static OWLModel protOWLModel;

	public static void main(String[] args) {
		try {

			String chosenDB = AGROVOC_INFO.current_agrovoc_small_DB;

			ARTModelFactoryProtegeImpl fact = new ARTModelFactoryProtegeImpl();

			model = (OWLModelProtegeImpl) fact.loadOWLModel("com.mysql.jdbc.Driver",
					"jdbc:mysql://127.0.0.1:3306/" + chosenDB
							+ "?requireSSL=false&useUnicode=true&characterEncoding=UTF-8", chosenDB,
					dbusername, dbpwd);

			protOWLModel = model.getProtegeOWLModel();

			String indexLocation = "./data/" + chosenDB + "/index";

			oi = new OWLSearchEngine(model, indexLocation, new StandardAnalysisConfiguration(Version.LUCENE_36, "en", "it"));

			ArrayList<String> indexes = new ArrayList<String>();
			indexes.add(c_nounInstancesIndexCategory);
			indexes.add(index_scopeNotes);
			indexes.add(index_ImgDescription);
			indexes.add(index_definition);

			oi.initializeCustomIndexWriters(indexes);

			oi.createOntIndexSearcher();

			// INDEX BUILDING (decomment this to redo indexing)
			// System.out.println("building index at: " + indexLocation);
			// ARTURIResource cnoun = model.createURIResource(commonAIMBaseNS + "c_noun");
			// ARTURIResourceIterator it = new
			// URIResourceIteratorFilteringResourceIterator(model.listInstances(
			// cnoun, false));
			// oi.buildCustomIndex(c_nounInstancesIndexCategory, it, true);
			// logger.info("INDEX BUILT");

			oi = new OWLSearchEngine(model, indexLocation, new StandardAnalysisConfiguration(Version.LUCENE_36, "en", "it", "es"));
			oi.initializeCustomIndexWriters(indexes);
			oi.createOntIndexSearcher();
			
			// testUpdateProperty();
			testCONTAINSSearch();

		} catch (Exception e) {
			e.printStackTrace(System.err);
			logger.error(e.getMessage());

			System.err.flush();
			System.err.println("error");

			e.printStackTrace();
		}
	}


	public static void testCONTAINSSearch() throws IOException, ModelAccessException, IndexAccessException, ParseException {
		System.err.println("\n\n\nNOW TESTING CONTAINS\n\n\n");

		Iterator<String> retrievedLabels;

		retrievedLabels = oi.findLabelsForIndex(c_nounInstancesIndexCategory, "*eina*", "es",
				OntIndexSearcher.EXACT_MATCH).iterator();
		while (retrievedLabels.hasNext())
			System.out.println(retrievedLabels.next());

		/*
		 * idxMgr.deleteResourceFromIndexes(newTerm, c_nounInstancesIndexCategory);
		 * 
		 * retrievedLabels = oi.findLabelsForIndex(c_nounInstancesIndexCategory, "termine", "it",
		 * OntIndexSearcher.FUZZY_MATCH).iterator(); while (retrievedLabels.hasNext())
		 * System.out.println(retrievedLabels.next());
		 */
	}

}
